﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using MethodWorx.CMS.Plugins.ActionResults;
using MethodWorx.CMS.Services;
using MethodWorx.CMS.Providers;
using System.Web.Mvc;
using MethodWorx.CMS.Configuration;
using MethodWorx.CMS.UI.Application.ActionResults;

namespace MethodWorx.CMS.Plugins.GlobalProperties
{
    public class GlobalPropertiesController : Controller
    {
        private IdeExtension extension;
        private ContentService content;
        private IEnumerable<IPropertyConfiguration> properties;
        private ICmsDataProvider provider;

        public GlobalPropertiesController(ICmsDataProvider provider, IdeExtension extension, ContentService content, IEnumerable<IPropertyConfiguration> properties)
        {
            this.extension = extension;
            this.content = content;
            this.properties = properties;
            this.provider = provider;
        }

        [HttpGet]
        public ActionResult Create()
        {
            return new EmbeddedViewResult(this.extension.WebEditor, new GlobalPropertiesModel(this.properties));
        }

        [HttpGet]
        public ActionResult Edit(Guid id)
        {
            PropertyDetails<object> propertyDetails = GetPropertyDetailsForEdit(id);

            if(propertyDetails != null)
            {
                var model = new GlobalPropertiesModel(this.properties, propertyDetails);

                return new EmbeddedViewResult(
                    new AssemblyResourceReference(Assembly.GetExecutingAssembly(),
                                                  "Views/GlobalProperties/Edit.ascx"), model);
            }
            else
            {
                throw new Exception("Can not find global property");
            }
        }

        private PropertyDetails<object> GetPropertyDetailsForEdit(Guid id)
        {
            var service = new PluginService(this.provider);
            var item = service.GetUserData<object>(id);
            PropertyDetails<object> propertyDetails = null;

            if (item != null)
                propertyDetails = (PropertyDetails<object>)item.GetType().InvokeMember("Cast", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance,
                                                                                       null, item, new object[] {typeof (object)});
            return propertyDetails;
        }

        [HttpPost]
        public ActionResult Edit(Guid id, PropertyDetails<object> [] propertyDetails)
        {
            var model = new GlobalPropertiesModel(this.properties, propertyDetails[0]);
            //
            //  ok, here we have our model,
            //  so its time to create the global property
            PluginService service = new PluginService(this.provider);
            service.SetUserData(id, model.PropertyDetails.Cast(model.Selected.GetDetailsType()));
            return new CloseDialogResult().And(new RefreshTreeNodeResult());
        }

        [HttpPost]
        public ActionResult Create(string submit,  PropertyDetails<object> [] propertyDetails)
        {
            var model = new GlobalPropertiesModel(this.properties, propertyDetails[0]);

            switch(submit)
            {
                case "save":
                    //
                    //  ok
                    if(string.IsNullOrEmpty(model.PropertyDetails.Name))
                        this.ModelState.AddModelError("Name", "This field is required");
                    
                    if(this.ModelState.IsValid)
                    {
                        //
                        //  ok, here we have our model,
                        //  so its time to create the global property
                        PluginService service = new PluginService(this.provider);
                        service.SetUserData(this.extension.Plugin, null, "GlobalProperty", model.PropertyDetails.Cast(model.Selected.GetDetailsType()), false);
                        return new CloseDialogResult().And(new RefreshTreeNodeResult());
                    }
                    break;
                case "cancel":
                    return new CloseDialogResult();
                    break;
            }

            return new EmbeddedViewResult(this.extension.WebEditor, this.ViewData, model);
        }
    }
}
